import {buchheim, Tree} from './Tree2';

export function getTree(array){
    return buchheim(staticTree(array));
}

const insert = (id, map) => {
    if (id === -1)
        return undefined;
    if (!(id in map)) {
        let temp = new Tree(id, undefined, undefined, id + "");
        map[id] = temp;
    }
    return map[id];
}

const staticTree = (array) => {
    let map = {};
    for (let item of array) {
        let root = insert(item[0], map);
        let left = insert(item[1], map);
        let right = insert(item[2], map);
        if (left)
            left.parent = root;
        if (right)
            right.parent = root;
        root.left = left;
        root.right = right;
    }
    let root;
    for (let i in map) {
        if (!map[i].parent) {
            root = map[i];
        }
    }
    return root;
}

